package com.jf.thread;

public class TicketPool {
    private int count = 100;

    public int getTicket(){
        /*同步代码快方式*/
        synchronized (this){
            if(count > 0){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                count--;
                return count;
            }
            System.out.println(Thread.currentThread().getName()+"已售空");
            return 0;
        }
    }

    /*同步方法*/
//    public synchronized int getTicket(){
//        if(count >0){
//            try {
//                Thread.sleep(10);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            count--;
//            return count;
//        }
//        return 0;
//    }


}
